#include "db/file.h"

void insertfiles(Client_t *client, MYSQL *conn, char *file_name, int type, long size, char *hash)
{
    char sql_select[512] = {0};
    char sql_insert[1024] = {0};
    MYSQL_RES *result = NULL;
    MYSQL_ROW row;

    // 拿到user_id
    sprintf(sql_select, "select user_id from users where username = '%s'", client->user_info.name);
    result = excutesql(sql_select, conn);
    row = mysql_fetch_row(result);
    int user_id = atoi(row[0]);
    mysql_free_result(result);

    printf("user_id: %d\n", user_id);

    // 拿到parent_id
    memset(sql_select, 0, sizeof(sql_select));
    sprintf(sql_select, "select file_id from files where file_path = '%s' and user_id = %d", client->cur_path, user_id);
    result = excutesql(sql_select, conn);
    row = mysql_fetch_row(result);
    int parent_id = atoi(row[0]);
    mysql_free_result(result);

    // printf("parent_id: %d\n", parent_id);

    // 获取文件的虚拟路径
    char path[512] = {0};
    if (type == 1)
    {
        sprintf(path, "%s%s/", client->cur_path, file_name);

        sprintf(sql_insert, "insert into files (user_id, parent_id, file_name, file_path, file_type) values(%d, %d, '%s', '%s', %d)",
                user_id, parent_id, file_name, path, type);
        result = excutesql(sql_insert, conn);
        mysql_free_result(result);
    }
    else
    {
        sprintf(path, "%s%s", client->cur_path, file_name);
        // printf("cur_path: %s\n", client->cur_path);
        sprintf(sql_insert, "insert into files (user_id, parent_id, file_name, file_path, file_type, file_size, hash) values(%d, %d, '%s', '%s', %d, %ld, '%s')",
                user_id, parent_id, file_name, path, type, size, hash);
        result = excutesql(sql_insert, conn);
        PRINT_DEBUG("insert file: %s\n", sql_insert);
        mysql_free_result(result);
    }

    // 插入文件信息
}
